//
//  ScanAnimationView.m
//  iOS 雷达动画
//
//  Created by 谭亚明 on 2020/6/18.
//  Copyright © 2020 GloryMan. All rights reserved.
//

#import "ScanAnimationView.h"
#import "TYMLabel.h"
#import "Masonry.h"
#import "Fonts.h"
#import "Colors.h"
@interface ScanAnimationView()

@property(nonatomic,weak) UIImageView  *bgImageView;
@property(nonatomic,weak) UIImageView *contImageView;
@property(nonatomic,weak) UIImageView *animImageView;
@property(nonatomic,weak) UIImageView *centerImage;

@property(nonatomic,weak) TYMLabel *progressLab;


@property(nonatomic,assign) BOOL isScaning;
@end

@implementation ScanAnimationView

-(instancetype)initWithFrame:(CGRect)frame{
    self  = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}



-(void)initUI{
    
    _isLinkSc = NO;
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 90)];
    [image setImage:[UIImage imageNamed:@"icon_img5"]];
    [self addSubview:image];
    _bgImageView = image;
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:image.frame];
    [image1 setImage:[UIImage imageNamed:@"icon_img1"]];
    [self addSubview:image1];
    _contImageView = image1;
    
    UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 176, 176)];
    [image2 setImage:[UIImage imageNamed:@"icon_img2"]];
    [self addSubview:image2];
    _animImageView = image2;
    image2.center = image.center;
    
    UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    image3.image = [UIImage imageNamed:@"icon_center"];
    [self addSubview:image3];
    image3.center = image2.center;
    _centerImage = image3;
    
    TYMLabel *lab = [TYMLabel lb_text:@"正在扫描设备…55%" font:Font_Medium(18) color:rgba(74, 74, 74)];
    [self addSubview:lab];
    lab.textAlignment = NSTextAlignmentCenter;
    _progressLab = lab;
    
    TYMLabel *titLab = [TYMLabel lb_text:@"请将设备尽量靠近路由器" font:Font_Regular(12) color:rgba(155, 155, 155)];
    titLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titLab];
    [titLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
    }];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(-22);
    }];
    self.isScaning = NO;
}

-(void)setScanAnimationCount:(NSInteger)count{
    _progressLab.text = [NSString stringWithFormat:@"%@…%ld%%",(!_isLinkSc)?@"正在扫描设备":@"设备配置路由成功",count];
}

-(void)setSuccessStr:(NSString*)str{
    _progressLab.text = @"设备配置路由成功";
}

-(BOOL)isScaning{
    return _isScaning;
}

-(void)starAnimation{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 2.5;
    animation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    animation.repeatCount = HUGE;
    animation.cumulative = false;
    [self.animImageView.layer addAnimation:animation forKey:@"rotationAnimation"];
    
    CABasicAnimation * animation1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation1.duration = 2;
    animation1.toValue = [NSNumber numberWithFloat:-M_PI * 2.0];
    animation1.repeatCount = HUGE;
    animation1.cumulative = false;
    [self.centerImage.layer addAnimation:animation1 forKey:@"rotationAnimation"];
    
    // 设定为缩放
    CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
     
    // 动画选项设定
    animation2.duration = 2.5; // 动画持续时间
    animation2.repeatCount = HUGE; // 重复次数
    animation2.autoreverses = YES;
    // 缩放倍数
    animation2.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
    animation2.toValue = [NSNumber numberWithFloat:1.2]; // 结束时的倍率
    // 添加动画
    [self.bgImageView.layer addAnimation:animation2 forKey:@"scale-layer"];
    
    [self.centerImage setImage:[UIImage imageNamed:@"icon_center"]];
    
    self.isScaning = YES;
}

-(void)stopAnimationScuess:(BOOL)scuess{
    self.isScaning = NO;
    [self.animImageView.layer removeAllAnimations];
    [self.centerImage.layer removeAllAnimations];
    [self.bgImageView.layer removeAllAnimations];
    if (!scuess) {
        [self.centerImage setImage:[UIImage imageNamed:@"loading2"]];
    }else{
        [self.centerImage setImage:[UIImage imageNamed:@"loading2"]];
    }
    
}

-(void)dealloc{
    NSLog(@"+++++++++++++++++搜索设备的动画释放");
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
